# 获取当前脚本所在目录
$currentDir = Split-Path -Parent $PSCommandPath
# 构建要追加的路径
$pythonScriptsPath = Join-Path $currentDir 'python\venv\Scripts'
$pythonPath = Join-Path $currentDir 'python\venv'

# 获取当前系统Path
$oldPath = (Get-ItemPropertyValue -Path 'HKLM:System\CurrentControlSet\Control\Session Manager\Environment' -Name Path)

# 检查并追加路径
$newPath = $oldPath, $pythonScriptsPath, $pythonPath -join ';'
if ($oldPath -notmatch [regex]::Escape($pythonScriptsPath)) {
    [Environment]::SetEnvironmentVariable('Path', $newPath, 'Machine')
    Write-Host 'Path appended successfully.'
} elseif ($oldPath -notmatch [regex]::Escape($pythonPath)) {
    # 如果Scripts路径已存在但主路径缺失，再次尝试追加主路径
    $newPath = $oldPath, $pythonPath -join ';'
    [Environment]::SetEnvironmentVariable('Path', $newPath, 'Machine')
    Write-Host 'Main path appended successfully.'
} else {
    Write-Host 'The paths were already present.'
}

Write-Host "Please check the system Path variable after a restart or signing out and back in to verify the change."